﻿using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.SignalR;
using System.Text;
using TrackSystem.UI.Server.Components;
using TrackSystem.UI.Server.Features;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

builder.Services.AddRazorComponents().AddInteractiveServerComponents();

builder.Services.AddBootstrapBlazor();

builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));

// 增加 Pdf 导出服务
builder.Services.AddBootstrapBlazorTableExportService();

// 增加 Html2Pdf 服务
builder.Services.AddBootstrapBlazorHtml2PdfService();

// 增加 SignalR 服务数据传输大小限制配置
builder.Services.Configure<HubOptions>(option => option.MaximumReceiveMessageSize = null);

builder.Services.AddScoped(
    sp => new HttpClient { BaseAddress = new Uri("http://localhost:5000") });

// 登录服务
builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthStateProvider>();
//builder.Services.AddScoped<CustomAuthStateProvider>(implementationFactory =>
//    implementationFactory.GetRequiredService<CustomAuthStateProvider>()
//);

builder.Services.AddRepositoryServices();

builder.Services.AddSignalR();

builder.Services.AddBootstrapBlazor(options =>
{
    options.ToastDelay = 4000;
});
builder.Services.AddECharts();
builder.Services.AddServerSideBlazor(options =>
{
    options.DisconnectedCircuitRetentionPeriod = TimeSpan.FromSeconds(30);
});

builder.Services.AddAuthentication();
builder.Services.AddAuthorization();


// -------------  运行服务 ------------------------------------------
var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
}

app.UseAntiforgery();

app.UseAuthentication();  // 启用身份验证
app.UseAuthorization();   // 登录授权

app.MapStaticAssets();
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();

app.Run();
